package com.pucheng.bracelet.app.base

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.View.inflate
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import com.pucheng.bracelet.R
import com.pucheng.bracelet.app.event.AppViewModel
import com.pucheng.bracelet.databinding.ActivityAboutBinding.inflate
import kotlinx.android.synthetic.main.activity_base_title.*
import kotlinx.android.synthetic.main.layout_title.*
import me.hgj.jetpackmvvm.base.activity.BaseVmActivity
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.demo.app.ext.dismissLoadingExt
import me.hgj.jetpackmvvm.demo.app.ext.showLoadingExt
import me.hgj.jetpackmvvm.ext.getAppViewModel

abstract class BaseTitleActivity<VM : BaseViewModel, DB : ViewDataBinding> : BaseVmActivity<VM>() {

    lateinit var mDatabind: DB
    //Application全局的ViewModel，里面存放了一些账户信息，基本配置信息等
    val appViewModel: AppViewModel by lazy { getAppViewModel<AppViewModel>() }

    override fun onCreate(savedInstanceState: Bundle?) {
        userDataBinding(true)
        super.onCreate(savedInstanceState)
    }

    /**
     * 创建DataBinding
     */
    override fun initDataBind() {
        mDatabind = DataBindingUtil.setContentView(this, R.layout.activity_base_title)
        mDatabind.lifecycleOwner = this
        ibtn_back.setOnClickListener { finish() }
        tv_title.text = titleText()
        (cl_connect as FrameLayout).addView(
            inflate(this, layoutId(), null),
            ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT
        )
    }

    /**
     * 创建liveData观察者
     */
    override fun createObserver() {}

    /**
     * 打开等待框
     */
    override fun showLoading(message: String) {
        showLoadingExt(message)
    }

    /**
     * 关闭等待框
     */
    override fun dismissLoading() {
        dismissLoadingExt()
    }

    abstract fun titleText(): String
}